package com.fuhailiu.opengl.widget;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLES30;

import com.fuhailiu.opengl.utils.GlUtil;
import com.fuhailiu.opengl.utils.LogUtil;
import com.fuhailiu.opengl.utils.MatrixUtil;
import com.fuhailiu.opengl.utils.ShaderUtil;
import com.fuhailiu.opengl.view.GLView;

public class GLBlend extends GLView {

	private final String TAG = getClass().getSimpleName();

	private final float x = 0.15f;
	private final float y = 0.15f;

	// 顶点坐标数据
	private float vertices[] = new float[] { 
			-x, -y, 0, // left bottom
			x, -y, 0, // right bottom
			-x, y, 0, // left top
			x, y, 0, // right top
	};

	// 顶点坐标数据缓冲
	private FloatBuffer mBufferVertex;

	// 自定义渲染管线程序id
	private int handleProgram;
	// 顶点位置属性引用id
	private int handlePosition;
	// 顶点颜色属性引用id
	private int handleColor;
	// 总变换矩阵引用id
	private int handleMVPMatrix;

	public GLBlend(Context context) {
		super(context);
		initVertexData();
		initShader();
	}

	private void initVertexData() {
		mBufferVertex = GlUtil.createFloatBuffer(vertices);
	}

	private static final String VERTEX_SHADER = 
			"uniform mat4 uMVPMatrix; //总变换矩阵\n" + 
			"attribute vec3 aPosition; //顶点位置\n" + 
			"uniform vec4 aColor; //顶点颜色\n" + 
			"varying vec4 vColor; //用于传递给片元着色器的变量\n" + 
			"void main()\n" + 
			"{\n" + 
			"	gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置\n" + 
			"	vColor = aColor; //将接收的颜色传递给片元着色器 \n" + 
			"}\n";

	private static final String FRAGMENT_SHADER = 
			"precision mediump float;\n" + 
			"varying vec4 vColor; //接收从顶点着色器过来的参数\n" + 
			"void main()\n" + 
			"{\n" + 
			"	gl_FragColor = vColor; //给此片元颜色值\n" + 
			"}\n";

	private void initShader() {
		LogUtil.LogD(TAG, "initShader   <---");

		// 基于顶点着色器与片元着色器创建程序
		handleProgram = ShaderUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
		// 获取程序中顶点位置属性引用id
		handlePosition = GLES30.glGetAttribLocation(handleProgram, "aPosition");
		// 获取程序中顶点颜色属性引用id
		handleColor = GLES30.glGetUniformLocation(handleProgram, "aColor");
		// 获取程序中总变换矩阵引用id
		handleMVPMatrix = GLES30.glGetUniformLocation(handleProgram, "uMVPMatrix");

		LogUtil.LogD(TAG, "initShader   --->");
	}

	public static final int BLEND_OFF = 0;
	public static final int BLEND_ON = 1;

	private int mBlendMode = BLEND_ON;

	// 混合方程式
	// Cf = Cs * S + Cd * D
	// Cf是最终计算产生的颜色，Cs是源颜色，Cd则是目标颜色，S和D分别是源和目标的混合因子

	public void setBlendtMode(int mode) {
		mBlendMode = mode;
		LogUtil.LogD(TAG, "setBlendtMode mode = " + mode);
	}

	// 注意alpha值为0.5f
	private final float red[] = { 1.0f, 0.0f, 0.0f, 0.5f };
	private final float green[] = { 0.0f, 1.0f, 0.0f, 1.0f };
	private final float blue[] = { 0.0f, 0.0f, 1.0f, 1.0f };
	private final float black[] = { 0.0f, 0.0f, 0.0f, 1.0f };

	private final float offsetX = 0.4f;
	private final float offsetY = 0.4f;

	public volatile float mTranslateX = 0;
	public volatile float mTranslateY = 0;

	private final int sizeOfVertexPosition = 3;

	public void draw(GL10 gl) {
		// 使用shader程序
		GLES30.glUseProgram(handleProgram);

		// 启用顶点位置数组
		GLES30.glEnableVertexAttribArray(handlePosition);
		// 启用顶点颜色数组
		GLES30.glEnableVertexAttribArray(handleColor);

		// 指定顶点位置数据
		GLES30.glVertexAttribPointer(handlePosition, sizeOfVertexPosition, GLES30.GL_FLOAT, false, sizeOfVertexPosition * GlUtil.SIZE_OF_FLOAT, mBufferVertex);

		// 画上下左右四个矩形
		drawRect(-offsetX, offsetY, red);
		drawRect(offsetX, offsetY, green);
		drawRect(-offsetX, -offsetY, blue);
		drawRect(offsetX, -offsetY, black);

		// 开启混合
		if (mBlendMode == BLEND_ON) {
			GLES30.glEnable(GLES30.GL_BLEND);
			// 指定使用的混合方程式，如果不指定，默认的就是ADD
			GLES30.glBlendEquation(GLES30.GL_FUNC_ADD);
			// 指定源和目标的混合因子
			GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
		}

		// Z冲突，为了呈现层叠效果，使用多边形偏移
		GLES30.glEnable(GLES30.GL_POLYGON_OFFSET_FILL);
		GLES30.glPolygonOffset(-1.0f, -1.0f);
		// 画中间可移动的矩形
		drawRect(mTranslateX, mTranslateY, red);
		GLES30.glDisable(GLES30.GL_POLYGON_OFFSET_FILL);

		// 关闭混合
		GLES30.glDisable(GLES30.GL_BLEND);

		// 禁用顶点位置数组
		GLES30.glDisableVertexAttribArray(handleColor);
		// 禁用顶点颜色数组
		GLES30.glDisableVertexAttribArray(handlePosition);
	}

	private void drawRect(float offsetX, float offsetY, float[] color) {
		// 备份
		MatrixUtil.pushMatrix();
		MatrixUtil.translate(offsetX, offsetY, 0);

		// 指定顶点颜色数据，使用单调着色
		GLES30.glUniform4fv(handleColor, 1, color, 0);

		// 指定变换矩阵
		GLES30.glUniformMatrix4fv(handleMVPMatrix, 1, false, MatrixUtil.getMVPMatrix(), 0);

		// 绘制三角形
		GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);

		// 还原
		MatrixUtil.popMatrix();
	}

}
